package org.inspursc.s0517.health.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.inspursc.s0517.health.common.dto.TempFilePathDTO;
import org.inspursc.s0517.health.common.service.FileDealService;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author B_Dream
 * @date 2018/5/29 19:45
 */
@RestController
@RequestMapping(value = "/file")
@Slf4j
@Api(value = "文件", description = "文件处理类")
public class FileDealController {




    @Autowired
    FileDealService dealService;

    @ApiOperation(value = "上传文件API")
    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> uploadsBatchFiles(HttpServletRequest request
    ) {
        try {
            List<TempFilePathDTO> str = dealService.uploadsBatchFiles(request);
            return ResponseUtil.success("上传成功！", str);
        }catch (Exception e){
            return ResponseUtil.error("上传失败！", false);
        }

    }

    @ApiOperation(value = "上传文件API")
    @RequestMapping(value = "/uploadLogoFile", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> uploadLogoFile(@RequestParam("file") MultipartFile file, HttpServletRequest request
    ) throws Exception {
        TempFilePathDTO str = dealService.uploadsFile(file,request);
        if (str.getUrl() == null ) {
            return ResponseUtil.error("上传失败！", false);
        } else {
            return ResponseUtil.success("上传成功！", str);
        }
    }


}
